package com.android.place360.weather;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.util.Log;
import com.android.place360.services.HttpService;

public class WeatherRequest {
	public static String REQUEST_URL = "http://www.google.com/ig/api?";
	public static String REQUEST_ERROR =  "Lack of required parameters";
	
	private String weather;
	private String hl;
	public WeatherRequest(String weather, String hl) {
		super();
		this.weather = weather;
		this.hl = hl;
	}
	
	public String sendRequest(){
		
		String request = getRequestString();
		if(request.equals(REQUEST_ERROR)) return REQUEST_ERROR;
		return HttpService.SendRequest(getRequestString());
		
	}
	
	public WeatherResponse getResponse(){
		try {
			URL url = new URL(getRequestString());
			HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
			int responseCode = httpCon.getResponseCode();
			
			if(responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpCon.getInputStream();
				
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				
				Document doc = db.parse(in);
				Element root = doc.getDocumentElement();
				
				
				NodeList forecastInfomation = root.getElementsByTagName("forecast_information");
				if (forecastInfomation == null)
					return null;
				Node node = forecastInfomation.item(0);
				String city = node.getChildNodes().item(0).getAttributes().item(0).getNodeValue();				
				
				CurrentCoditions cc = new CurrentCoditions();
				
				//get current conditions
				node = root.getElementsByTagName("current_conditions").item(0);
				NodeList nodeList = node.getChildNodes();
				String condition = nodeList.item(0).getAttributes().item(0).getNodeValue();
				String temp_f = nodeList.item(1).getAttributes().item(0).getNodeValue();
				String temp_c = nodeList.item(2).getAttributes().item(0).getNodeValue();
				String humidity = nodeList.item(3).getAttributes().item(0).getNodeValue();
				String icon = nodeList.item(4).getAttributes().item(0).getNodeValue();
				String wind_condition = nodeList.item(5).getAttributes().item(0).getNodeValue();
				
				cc.setCondition(condition);
				cc.setTemp_c(temp_c);
				cc.setTemp_f(temp_f);
				cc.setIcon(icon);
				cc.setHumidity(humidity);
				cc.setWind_condition(wind_condition);
				ArrayList<ForecastConditions> lstFC = new ArrayList<ForecastConditions>();
				//forecast_conditions
				nodeList = root.getElementsByTagName("forecast_conditions");
				for (int i=0; i< nodeList.getLength(); i++) {
					NodeList lst_forecast = nodeList.item(i).getChildNodes();
					
					ForecastConditions fc = new ForecastConditions();
					fc.setDay_of_week(lst_forecast.item(0).getAttributes().item(0).getNodeValue());
					fc.setLow(lst_forecast.item(1).getAttributes().item(0).getNodeValue());
					fc.setHigh(lst_forecast.item(2).getAttributes().item(0).getNodeValue());
					fc.setIcon(lst_forecast.item(3).getAttributes().item(0).getNodeValue());
					fc.setCondition(lst_forecast.item(4).getAttributes().item(0).getNodeValue());
					lstFC.add(fc);
				}
				
				WeatherResponse wResponse = new WeatherResponse();
				wResponse.setCurrent_conditions(cc);
				wResponse.setCity(city);
				wResponse.setForecast_conditions(lstFC);
				return wResponse;
				
			}
		} catch (MalformedURLException e) {
			Log.e("place360", e.getMessage());			
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}
	
	public String getRequestString(){
		String request = REQUEST_URL;
		if(!parameterValid(weather)) 	return REQUEST_ERROR;
		request += "weather=" + weather;
		if(parameterValid(hl)) 	request += "&hl=" + hl;
		request = request.replace(" ", "%20").trim();
		
		return request;
	}
	
	private boolean parameterValid(String para){
		if(para==null || para.trim().equals("")) return false;
		return true;
	}
	
	public String getWeather() {
		return weather;
	}
	public void setWeather(String weather) {
		this.weather = weather;
	}
	public String getHl() {
		return hl;
	}
	public void setHl(String hl) {
		this.hl = hl;
	}
	
	
}
